from django.shortcuts import render, redirect
# 导包
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
# 导入类视图
from django.views import View

import json
from django.core.serializers import serialize
from rest_framework.response import Response
from rest_framework.views import APIView
# 导入加密库
import hashlib
# 导入图片库
# 绘画库
from PIL import ImageDraw
# 字体库
from PIL import ImageFont
# 图片库
from PIL import Image
# 随机库
import random
# 文件流
import io

import requests

# 导入上传文件夹配置
from mydjango.settings import UPLOAD_ROOT
import os

# 导入原生sql模块
from django.db import connection

import jwt

# 导入redis数据库
import redis

# 导入时间模块
import time

# 导入公共目录变量
from mydjango.settings import BASE_DIR

# 导包
from django.db.models import Q, F

# 导入dwebsocket的库
from dwebsocket.decorators import accept_websocket
import uuid

from myapp.models import Users

from django.utils.deprecation import MiddlewareMixin

from django.http import QueryDict

# 类视图调用
from django.utils.decorators import method_decorator

# 事务操作
from django.db import transaction


# 用户注册
# class Registers(APIView):
#     def post(self, request):
#         # 接收参数
#         username = request.data.get('username', None)
#         password = request.data.get('password', None)
#         phone = request.data.get('phone', None)
#         email = request.data.get('email', None)
#         # #拍重操作
#         user = Users.objects.filter(username=username).first()
#         if user:
#             res = {}
#             res['code'] = 405
#             res['message'] = '用户已经存在'
#             return Response(res)
#         # 入库
#         try:
#             print(username, password, phone)
#             user = Users(username=username, password=password, phone=phone, email=email)
#             user.save()
#             return Response({"code": 200, 'message': '注册成功'})
#         except Exception as e:
#             print(str(e))
#             return Response({'code': 405, 'message': '注册失败'})


class Login(APIView):
    def post(self, request):
        # 接收参数
        username = request.POST.get('username', None)
        password = request.POST.get('password', None)
        phone = request.POST.get('phone', None)
        email = request.POST.get('email', None)

        # 判断登录方式
        if username == None: #如果用户名为空
            if email == None:
                user = Users.objects.filter(phone=phone, password=password).first()
            else:
                user = Users.objects.filter(email=email, password=password).first()
        else:
            user = Users.objects.filter(username=username, password=password).first()





        # 返回结果
        if user:
            return Response({'code': 200, 'message': '登陆成功'})
        else:
            return Response({'code': 405, 'message': '用户名活密码错误'})

        # try:
        #     print(username, password, phone,email)
        #     user = User.objects.get(Q(username=username) | Q(phone=username) | Q(email=username), password=password)
        #     return Response({"code": 200, 'message': '登录成功', 'uid': user.id})
        # except Exception as e:
        #     print(str(e))
        #     return Response({'code': 405, 'message': '登录失败'})
